// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    order_goods_list: [],
    network: getApp().globalData["network"],
    selected: 1
  },
  query: function (id) {
    let that = this;
    // 查询结果
    wx.request({
      url: "http://" + that.data.network + "/front/queryOrder.php",
      method: "post",
      data: {
        user_id: id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        that.setData({
          order_goods_list: data.data
        });
      }
    });
  },
  // 显示全部 完成的订单 --------------------等会做
  queryAll: function (event) {
    var index = event.currentTarget.dataset.index;
    var that= this;
    console.log(index);
    this.setData({
      selected:index
    });
    wx.request({
      url: "http://" + that.data.network + "/front/queryallOrder.php",
      method: "post",
      data: {
        user_id: that.data.id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        that.setData({
          order_goods_list:data.data
        });
      }
    });
  },
  //查询带付款的函数
  querynopayfun: function (id) {
    var that = this;
    wx.request({
      url: "http://" + that.data.network + "/front/queryOrder.php",
      method: "post",
      data: {
        user_id: id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        that.setData({
          order_goods_list: data.data
        });
      }
    });

  },
  //查找待付款事件
  querynopay: function (event) {
    var that = this;
    var index = event.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      selected: index
    });
    this.querynopayfun(that.data.id);
  },

  //查找待发货
  querynosend: function (event) {
    var that = this;
    var index = event.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      selected: index
    });
    wx.request({
      url: "http://" + that.data.network + "/front/querypayOrder.php",
      method: "post",
      data: {
        user_id: that.data.id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        that.setData({
          order_goods_list: data.data
        });
      }
    });
  },
  // 查找待收函数
  querynoreceivefun:function(id){
    var that = this;
    wx.request({
      url: "http://" + that.data.network + "/front/querysendOrder.php",
      method: "post",
      data: {
        user_id: id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        that.setData({
          order_goods_list: data.data
        });
      }
    });
  },
  // 查找待收货事件
  querynoreceive: function (event) {
    var that = this;
    var index = event.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      selected: index
    });
    this.querynoreceivefun(that.data.id);
    
  },
  //付款
  topay: function (event) {
    var index = event.currentTarget.dataset.index;
    var that = this;
    wx.showModal({
      title: '警告',
      content: '由于开发时间原因，未加入支付真实货币功能，仅会扣除帐户余额！您确定要支付吗？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: "http://" + that.data.network + "/front/topay.php",
            method: "post",
            data: {
              order_id: index
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (data) {
              console.log(data);
              that.querynopayfun(that.data.id);
              wx.showToast({
                title:"付款成功",
                icon:"success",
                duration:1000
              }); 
            }
          });

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  //取消订单
  cancel: function (event) {
    var that = this;
    var order_id = event.currentTarget.dataset.index;
    wx.request({
      url: "http://" + that.data.network + "/front/cancelOrder.php",
      method: "post",
      data: {
        order_id: order_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        that.querynopayfun(that.data.id);
      }
    });

  },
  // 收货
  toshou: function (event) {
    var order_id = event.currentTarget.dataset.index;
    var that = this;
    console.log(order_id);
    wx.request({
      url: "http://" + that.data.network + "/front/toshou.php",
      method: "post",
      data: {
        order_id: order_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        console.log(data);
        that.querynoreceivefun(that.data.id);
        wx.showToast({
          title:"收货成功",
          icon:"none",
          duration:1000
        });
        
      }
    });
  },
  todtl:function(event){
    var index =event.currentTarget.dataset.index;
    console.log(index);
    wx.navigateTo({
      url:"/pages/detail/detail",
      events:{
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
         // console.log(data)
        },
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage',index)
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var index = wx.getStorageSync("id");
    console.log("id=" + index);
    this.setData({
      id: index
    });
    this.query(index);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

})